﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.DA;
using System.Linq;
using Com.BU;

namespace Com
{
    /// <summary>
    /// Interaction logic for ucColdStorageWall.xaml
    /// </summary>
    public partial class ucColdStorageWall : UserControl
    {
        public ucColdStorageWall()
        {
            this.InitializeComponent();
            enableButtons();
            loadData();
        }

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {


        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //  So sánh loại vật liệu tương được nhập vào
                //  Nếu là loại vật liệu được nhập mới thì cập nhật vào bảng loại tường


                tb_temperature_excess te = new tb_temperature_excess();
                te.description = cbxDescription.Text;
                te.wall_material_id = ((tb_wall_material)cbxType.SelectedItem).id;
                te.direction_id = ((tb_wall_direction)cbxDirection.SelectedItem).id;
                te.termperature_excess = Convert.ToInt16(txtTemperature.Text);
                
                // Kiem tra item da ton tai trong csdl chua
                tb_temperature_excess teTest = LibraryManagerInCom.TemperatureExcessList.SingleOrDefault(t => t.wall_material_id == te.wall_material_id && t.direction_id == te.direction_id);
                if (teTest == null)
                {
                    //  Luu item và load lai bang du lieu
                    LibraryManagerInCom.addTemperatureExcessList(te);
                    enableButtons();
                    loadData();
                }
                else
                {
                    MessageBox.Show("This item has been already exist!","Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                }

                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }

        private void loadData()
        {

            loadDirection();
            loadWallType();
            loadDescription();

            tb_temperature_excess te = (tb_temperature_excess)cbxDescription.SelectedItem;
            cbxType.SelectedItem = LibraryManagerInCom.CSWallMaterialList.SingleOrDefault(w => w.id == te.wall_material_id);
            cbxDirection.SelectedItem = LibraryManagerInCom.wallDirectionList.SingleOrDefault(d => d.id == te.direction_id);
            txtTemperature.Text = te.termperature_excess.ToString();
        }



        private void btnNew_Click(object sender, RoutedEventArgs e)
        {
            cbxDescription.Text = "";
            cbxType.SelectedIndex = 0;
            txtTemperature.Text = "";
            cbxDirection.SelectedIndex = 0;
            disableButtons();

        }

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            tb_temperature_excess te = (tb_temperature_excess)cbxDescription.SelectedItem;
            tb_temperature_excess t2 = LibraryManagerInCom.CopyTemperatureExcess(te);

            cbxDescription.Text = t2.description;
            cbxDirection.SelectedItem = (tb_wall_direction)LibraryManagerInCom.wallDirectionList.SingleOrDefault(d => d.id == t2.direction_id);
            cbxType.SelectedItem = (tb_wall_material)LibraryManagerInCom.CSWallMaterialList.SingleOrDefault(w => w.id == t2.wall_material_id);
            txtTemperature.Text = t2.termperature_excess.ToString();
            disableButtons();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            tb_temperature_excess te = (tb_temperature_excess)cbxDescription.SelectedItem;
            if (MessageBox.Show("Do you want to delete this item?", "Confirm", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
            {
                LibraryManagerInCom.delTemperatureExcess(te);
                loadData();
            }
        }

        private void btnNew_Copy_Click(object sender, RoutedEventArgs e)
        {
            enableButtons();
            loadData();

        }

        private void enableButtons()
        {
            btnCancel.IsEnabled = false;
            btnSave.IsEnabled = false;

            btnNew.IsEnabled = true;
            btnDelete.IsEnabled = true;
            btnCopy.IsEnabled = true;

            //cbxType.IsEditable = false;

        }
        private void disableButtons()
        {
            btnCancel.IsEnabled = true;
            btnSave.IsEnabled = true;

            btnNew.IsEnabled = false;
            btnDelete.IsEnabled = false;
            btnCopy.IsEnabled = false;

            //cbxType.IsEditable = true;

        }
        private void loadWallType()
        {
            cbxType.Items.Clear();
            LibraryManagerInCom.loadCSWallMaterialList();
            foreach (var item in LibraryManagerInCom.CSWallMaterialList)
            {
                cbxType.Items.Add(item);
            }
            cbxType.DisplayMemberPath = "wall_type";
            cbxType.SelectedIndex = 0;
        }

        private void loadDirection()
        {
            cbxDirection.Items.Clear();
            LibraryManagerInCom.loadDirection();
            foreach (var item in LibraryManagerInCom.wallDirectionList)
            {
                cbxDirection.Items.Add(item);
            }
            cbxDirection.DisplayMemberPath = "direction";
            cbxDirection.SelectedIndex = 0;
        }
        private void loadDescription()
        {
            cbxDescription.Items.Clear();
            LibraryManagerInCom.loadTemperatureExcessList();
            foreach (var item in LibraryManagerInCom.TemperatureExcessList)
            {
                cbxDescription.Items.Add(item);
            }
            cbxDescription.DisplayMemberPath = "description";
            cbxDescription.SelectedIndex = 0;
        }

        private void cbxDescription_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxDescription.SelectedItem != null)
            {
                tb_temperature_excess te = (tb_temperature_excess)cbxDescription.SelectedItem;
                cbxType.SelectedItem = LibraryManagerInCom.CSWallMaterialList.SingleOrDefault(w => w.id == te.wall_material_id);
                cbxDirection.SelectedItem = LibraryManagerInCom.wallDirectionList.SingleOrDefault(d => d.id == te.direction_id);
                txtTemperature.Text = te.termperature_excess.ToString();
            }
            else
            {
                cbxType.SelectedIndex = 0;
                cbxDirection.SelectedIndex = 0;
                txtTemperature.Text = "";
            }

        }
    }
}